//定义一些基本变量
var base_domain = "/Baibai";

function getElementById(elementId) {
	return document.getElementById(elementId);
}

// string去除开头和结尾的空格
String.prototype.trim = function() {
	var s = this.replace(/(^\s*)/g, "");
	return s.replace(/(\s*$)/g, "");
};


/**
 * 字符串裁剪<br>
 * last = (str.length<=n?str.length:n) <br>
 * return str.substr(0,last-1);
 * 
 * @param n
 */
String.prototype.cut = function(n) {
	var length = this.length;
	var last = (length <= n ? length : n);
	
	return this.substring(0, last);
};

/**
 * 传入一个字符串，去除‘\n’, 根据每行的字数，插入‘<br>’
 * 
 * @param str
 * @param n
 *            每行的字数
 */
function strPagination(str, n) {
	// 去除“\n”
	while (true) {
		if (str.indexOf("\n") != -1) {
			str.replace("\n", "");
		} else {
			break;
		}
	}

	var result = "";
	var rows = Math.floor(str.length / n) + 1;

	for ( var i = 0; i < rows; i++) {
		if (i == rows - 1) {
			result += (str.substring(i * n, (i + 1) * n) + "  ");
		} else {
			result += (str.substring(i * n, (i + 1) * n) + "<br>");
		}

	}

	return result;
};

/**
 * 检查邮箱
 */
function checkEmail(email, showFont) {

	// 邮箱格式的正则表达式
	var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	var html = "";
	var checkEmailResult = false;

	//
	if (email == "" || email == undefined) {
		html = "邮箱不能为空";
		checkEmailResult = false;
	}
	// 不符合邮箱的格式
	else if (!email.match(pattern)) {
		html = "邮箱的格式不正确";
		checkEmailResult = false;
	}
	// 正确
	else {
		// html="正确";
		checkEmailResult = true;
		if (!$(".tips").hasClass("hide")) {
			$(".tips").addClass("hide");
		}
		return true;
	}
	// 将结果隐藏在前台
	// html += "<input type='hidden' id='checkEmailResult'
	// value='"+checkEmailResult+"' />";

	if ($(".tips").hasClass("hide")) {
		$(".tips").removeClass("hide");
	}
	$("#" + showFont).text(html);
	return checkEmailResult;
}

/**
 * 验证身份号码的合法性 第十八位数字的计算方法为： 1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为：7 9 10 5 8
 * 4 2 1 6 3 7 9 10 5 8 4 2 2.将这17位数字和系数相乘的结果相加。 3.用加出来和除以11，看余数是多少？ 4余数只可能有0 1
 * 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。
 * 5.通过上面得知如果余数是2，就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10，身份证的最后一位号码就是2。
 */

/**
 * ids:身份证号 showFont：展示信息的id名
 */
function checkId(ids, showFont) {
	var series = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
	var checkCodes = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4",
			"3", "2");
	showArea = $("#" + showFont);
	showArea.empty();
	var html = "";
	var checkIdResult = false;

	if (ids == null || ids == "" || ids == undefined) {
		html = "身份证不能为空";
	} else if (ids.length != 18) {
		html = "身份证应为18位";
	}

	else {
		var result = 0;
		var reminder = 0;
		for ( var i = 0; i <= 16; i++) {
			var n = parseInt(ids[i], 10);
			result += n * series[i];
		}
		reminder = result % 11;
		if (reminder == 2 && (ids[17] == "X" || ids[17] == "x")) {
			html = "<img src='../images/register/true.png' class='r-true'>";
			checkIdResult = true;
		} else if (reminder != 2 && checkCodes[reminder] == ids[17]) {
			html = "<img src='../images/register/true.png' class='r-true'>";
			checkIdResult = true;
		} else {
			html = "身份证号码不合法";
		}
	}
	// //将结果隐藏在前台
	// $("#checkIdResult").val(""+checkIdResult);
	// 在此插入错误提示栏
	showArea.append(html);

	return checkIdResult;
}

/*
 * 功能：保存cookies函数 参数：name，cookie名字；value，值
 */
function SetCookie(name, value) {
	var Days = 10; // cookie 将被保存10天
	var exp = new Date(); // 获得当前时间
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); // 换成毫秒
	document.cookie = name + "=" + escape(value) + ";expires="
			+ exp.toGMTString();
}

/*
 * 功能：获取cookies函数 参数：name，cookie名字
 */
function getCookie(name) {
	var arr = document.cookie
			.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null) {
		return unescape(arr[2]);
	}
	return null;

}

/*
 * 功能：删除cookies函数 参数：name，cookie名字
 */
function delCookie(name) {
	var exp = new Date(); // 当前时间
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if (cval != null) {
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
	}
}

/**
 * 返回值为n的index
 * 
 * @param n
 * @returns {Number}
 */
Array.prototype.indexOf = function(n) {
	for ( var i = 0; i < this.length; i++) {
		if (n == this[i]) {
			return i;
		}
	}
	return -1;
};
/**
 * 删除指定一个值为n的数
 * 
 * @param n
 */
Array.prototype.remove = function(n) {
	var index = this.indexOf(n);
	if (index != -1) {
		this.splice(index, 1);
	}
};

/**
 * 清空
 */
Array.prototype.removeAll = function() {
	this.splice(0, this.length);
};

/**
 * 是否存在该值<br>
 * 存在:true 不存在:false
 * 
 * @param n
 */
Array.prototype.contains = function(n) {
	if (this.indexOf(n) != -1) {
		return true;
	}
	return false;
};
/**
 * 通过Index删除
 * 
 * @param index
 */
Array.prototype.removeByIndex = function(index) {
	if (index < 0 || index >= this.length) {
		return;
	}

	this.splice(index, 1);
};

// Converts an integer (unicode value) to a char
function itoa(i) {
	return String.fromCharCode(i);
}

// Converts a char into to an integer (unicode value)
function atoi(a) {
	return a.charCodeAt();
}

Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	};
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
	return format;
};

/**
 * 得到当前时间：yyyy-MM-dd HH:mm
 */
function getTime() {
	return new Date().format("yyyy-MM-dd hh:mm");
}


/**
 * 全屏
 */
/* 
Native FullScreen JavaScript API
-------------
Assumes Mozilla naming conventions instead of W3C for now
*/

(function() {
	var 
		fullScreenApi = { 
			supportsFullScreen: false,
			isFullScreen: function() { return false; }, 
			requestFullScreen: function() {}, 
			cancelFullScreen: function() {},
			fullScreenEventName: '',
			prefix: ''
		},
		browserPrefixes = 'webkit moz o ms khtml'.split(' ');
	
	// check for native support
	if (typeof document.cancelFullScreen != 'undefined') {
		fullScreenApi.supportsFullScreen = true;
	} else {	 
		// check for fullscreen support by vendor prefix
		for (var i = 0, il = browserPrefixes.length; i < il; i++ ) {
			fullScreenApi.prefix = browserPrefixes[i];
			
			if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined' ) {
				fullScreenApi.supportsFullScreen = true;
				
				break;
			}
		}
	}
	
	// update methods to do something useful
	if (fullScreenApi.supportsFullScreen) {
		fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';
		
		fullScreenApi.isFullScreen = function() {
			switch (this.prefix) {	
				case '':
					return document.fullScreen;
				case 'webkit':
					return document.webkitIsFullScreen;
				default:
					return document[this.prefix + 'FullScreen'];
			}
		}
		fullScreenApi.requestFullScreen = function(el) {
			return (this.prefix === '') ? el.requestFullScreen() : el[this.prefix + 'RequestFullScreen']();
		}
		fullScreenApi.cancelFullScreen = function(el) {
			return (this.prefix === '') ? document.cancelFullScreen() : document[this.prefix + 'CancelFullScreen']();
		}		
	}

	// jQuery plugin
	if (typeof jQuery != 'undefined') {
		jQuery.fn.requestFullScreen = function() {
	
			return this.each(function() {
				var el = jQuery(this);
				if (fullScreenApi.supportsFullScreen) {
					fullScreenApi.requestFullScreen(el);
				}
			});
		};
	}

	// export api
	window.fullScreenApi = fullScreenApi;	
})();



/**
 * 去除句子中的多个空格==》 一个空格
 */
function cutSpace(s)
{
    var len = s.length;
    var i;
    var ch;
    var result = "";

    i = 0;
    while (i < len)
    {
        ch = s.charAt(i++);
        result += ch;
        if (ch != ' ')
        {
            continue;
        }

        while (i < len && (ch = s.charAt(i)) == ' ')
        {
           i++;
        }
    }

    return result;
}